<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: 肖林
  Date: 2018/3/20
  Time: 15:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title> </title>
    <style type="text/css">
      table {
        border: 1px solid #000;
        border-collapse: collapse;
      }

      td, th {
        border: 1px solid #000;
        padding: 10px;
      }
    </style>
  </head>
  <body>
    <table>
      <thead>
      <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>成绩</td>
      </tr>
      </thead>
      <tbody>

      <%
        class user{
            int id;
            String name;
            float score;

          public  user(int id, String name,float score)
          {
              this.id=id;
              this.name=name;
              this.score=score;
          }
        }

        List<user>list = new ArrayList<>();
        Connection connection=null;
        Statement statement= null;
        ResultSet resultSet=null;

        String sql="select * from lab01";
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/java_2017?serverTimezone=Asia/Shanghai",
                "root",
                "195122"
        );
        statement = connection.createStatement();

        resultSet = statement.executeQuery(sql);

        while(resultSet.next()){

          int id = resultSet.getInt(1);
          String name = resultSet.getString(2);
          float score = resultSet.getFloat(3);

          user user = new user(id, name, score);
          list.add(user);

        }
        resultSet.close();
        statement.close();
        connection.close();

        for (user user:list){
            out.write("<tr> <td>"+user.id+"</td> <td>"+user.name
                    +"</td> <td>"+user.score+"</td> </tr>");
        }
      %>
      </tbody>
    </table>

  </body>
</html>
